package cn.itcast.n7;

import lombok.extern.slf4j.Slf4j;

import java.util.function.BiFunction;

/**
 * className: Test2
 * description:
 * date: 2023/2/28-11:19
 * <p>
 * project: concurrent
 * package: cn.itcast.n7
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
@Slf4j(topic = "c.Test2")
public class Test2 {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                int sum = sum((a, b) -> a + b, 100, -10, 1);
            }).start();
        }
    }

    public static int sum(BiFunction<Integer, Integer, Integer> function, int init, int amount, int n) {
        if (n == 1) {
            return function.apply(init, amount);
        }
        return sum(function, function.apply(init, amount), amount, n - 1);
    }
}

class Accumulator implements BiFunction<Integer, Integer, Integer> {

    /**
     * Applies this function to the given arguments.
     *
     * @param a the first function argument
     * @param b the second function argument
     * @return the function result
     */
    @Override
    public Integer apply(Integer a, Integer b) {
        return a + b;
    }
}

